package com.hospital.register.core.controller.website;

import com.hospital.register.common.exception.BusinessException;
import com.hospital.register.common.result.Result;
import com.hospital.register.common.result.ResultCodeEnum;
import com.hospital.register.core.service.ScheduleService;
import com.hospital.register.dto.core.common.ScheduleDaysDto;
import com.hospital.register.dto.core.common.SchedulePageDto;
import com.hospital.register.vo.core.api.ScheduleDetailVo;
import com.hospital.register.vo.core.common.ScheduleDaysVo;
import com.hospital.register.vo.core.common.ScheduleQueryVo;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.Range;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "排班信息", description = "面向用户")
@RestController
@RequestMapping("/api/hosp/hospital")
@Validated
public class ScheduleSiteController {

    /**
     * 处理排班相关业务
     */
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation(value = "获取指定科室未来几天的排班信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", required = true, paramType = "path", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true, paramType = "path", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "hoscode", value = "医院code", required = true, paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "depcode", value = "科室code", required = true, paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/auth/getBookingScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public Result<ScheduleDaysVo> getBookingSchedule(
            @PathVariable @Min(value = 1, message = "页码不可小于 1") Integer page,
            @PathVariable @Range(min = 1L, max = 15L, message = "每页条数不可超过 15") Integer limit,
            @PathVariable String hoscode,
            @PathVariable String depcode) {
        ScheduleDaysDto conditions = new ScheduleDaysDto();
        conditions.setHoscode(hoscode);
        conditions.setDepcode(depcode);
        conditions.setPage(page);
        conditions.setLimit(limit);

        ScheduleDaysVo resultData = this.scheduleService.findFutureData(conditions);
        resultData.setBookingScheduleRuleList(null);//其它接口用的
        return Result.ok(resultData);
    }

    @ApiOperation(value = "获取某科室指定日期的排班数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "hoscode", value = "医院code", required = true, paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "depcode", value = "科室code", required = true, paramType = "path", dataTypeClass = String.class),
            @ApiImplicitParam(name = "workDate", value = "排班日期", required = true, paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
    public Result<List<ScheduleQueryVo>> findScheduleList(
            @PathVariable String hoscode,
            @PathVariable String depcode,
            @PathVariable String workDate) {

        DateTime dateTime = null;
        try {
            dateTime = new DateTime(workDate);
        } catch (Exception e) {
            throw new BusinessException("日期格式错误", ResultCodeEnum.PARAM_ERROR.getCode());
        }

        SchedulePageDto conditions = new SchedulePageDto();
        conditions.setPage(1);
        conditions.setLimit(20);
        conditions.setHoscode(hoscode);
        conditions.setDepcode(depcode);
        conditions.setWorkDate(dateTime.toDate());

        Page<ScheduleQueryVo> page = this.scheduleService.findPage(conditions);
        return Result.ok(page.getContent());
    }

    @ApiOperation("根据 id 查询排班")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scheduleId", value = "排班的 id", required = true, paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/getSchedule/{scheduleId}")
    public Result getSchedule(
            @PathVariable @NotBlank(message = "排班 id 不可为空") String scheduleId) {
        ScheduleQueryVo schedule = this.scheduleService.findById(scheduleId);
        return Result.ok(schedule);
    }

    @ApiOperation(value = "根据 id 查询排班详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "scheduleId", value = "排班的 id", required = true, paramType = "path", dataTypeClass = String.class)
    })
    @GetMapping("/inner/getScheduleOrderVo/{scheduleId}")
    public ScheduleDetailVo getScheduleOrderVo(
            @ApiParam(name = "scheduleId", value = "排班id", required = true)
            @PathVariable("scheduleId") @NotBlank(message = "排班 id 不可为空")
                    String scheduleId) {
        return this.scheduleService.findDetailById(scheduleId);
    }

}
